#ifndef SOCKET_H
#define SOCKET_H
#include <QProcess>
#include <QTcpSocket>
#include <QTcpServer>
#include <QHostAddress>
#include <QDebug>

class socket : public QObject
{
    Q_OBJECT

public:
QTcpSocket *tcp;
    const QString LOCAL_PORT = "12580";
    //static const QString LOCAL_PORT;
    socket();
    socket(QString adbPath, QString remotePort, QObject *parent = nullptr);


    QString request(const QString& msg)
    {
        if (!m_socket || !m_socket->isOpen())
            return "连接失败！";

        sendRequest();
        return receive();
    }

//private:
    QString m_adbPath;
    QString m_remotePort;
    QTcpSocket* m_socket;
    void connectToDevice()
    {
        QProcess process;
        QString adbCommand = QString("%1 forward tcp:%2 tcp:%3").arg(m_adbPath, LOCAL_PORT, m_remotePort);
        process.start(adbCommand);
        process.waitForFinished(-1);

        if (process.exitCode() == 0) {
            m_socket = new QTcpSocket(this);
            connect(m_socket, &QTcpSocket::connected, this, &socket::sendRequest);
            connect(m_socket, &QTcpSocket::readyRead, this, &socket::readResponse);
            connect(m_socket, QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::error), this, &socket::handleError);

            QHostAddress hostAddress(QHostAddress::LocalHost);
            m_socket->connectToHost(hostAddress, LOCAL_PORT.toInt());
        }
    }

    void sendRequest()
    {
        QString msg = "Your message"; // Replace with the actual message
        QByteArray requestData = msg.toUtf8();
        m_socket->write(requestData);
        m_socket->flush();
    }

    QString receive()
    {
        QString response;
        while (m_socket->waitForReadyRead()) {
            QByteArray responseData = m_socket->readAll();
            response += QString::fromUtf8(responseData);
        }
        return response;
    }

    void handleError(QAbstractSocket::SocketError socketError)
    {
        qDebug() << "Socket error:" << socketError;
    }

    void readResponse()
    {
        QString response = QString::fromUtf8(m_socket->readAll());
        qDebug() << "Received response:"<< response;
    }

    void TCPchile()
    {
        QString IP="192.168.2.71";
        quint16 port = 9527;
        QTcpSocket* m_tcp;
        QHostAddress address(IP);
        m_tcp=new QTcpSocket(this);
        m_tcp->connectToHost(address,port);

//        connect(m_tcp, &QTcpSocket::readyRead, this,[=]()
//        {
//            QByteArray data =m_tcp->readAll();
//            qDebug()<<"date";
//            qDebug()<<"读取数据";
//        });

//        connect(m_tcp, &QTcpSocket::disconnected, this,[=]()
//        {
//            m_tcp->close();
//            m_tcp->deleteLater();
//            qDebug()<<"断开连接";

//        });
        connect(m_tcp, &QTcpSocket::connected, this,[=]()
        {
            qDebug()<<"连接成功";
            QString mas="11";
            m_tcp->write(mas.toUtf8());
        });

        connect(m_tcp, static_cast<void(QAbstractSocket::*)(QAbstractSocket::SocketError)>(&QTcpSocket::error), this, [=](QAbstractSocket::SocketError socketError) {
            qDebug() << "连接错误:" << socketError;
            qDebug() << "错误描述:" << m_tcp->errorString();
        });

        qDebug() << "连接状态:" << m_tcp->state();

    }
    void Tcpsever()
    {

        QTcpServer* m_tcp;


        m_tcp=new QTcpServer(this);

        //监听

        unsigned short port=1122;
        m_tcp->listen(QHostAddress::Any,port);

        //如果有新的连接
        connect(m_tcp,&QTcpServer::newConnection,this,[=]()
        {
            //创建一个新的套接字对象
             tcp = m_tcp->nextPendingConnection();
            //读取数据
            connect(tcp,&QTcpSocket::readyRead,this,[=]()
            {
                QByteArray data =tcp->readAll();
                qDebug()<<data;
            });
            connect(tcp, &QTcpSocket::disconnected, this,[=]()
            {
                tcp->close();
                tcp->deleteLater();
                qDebug()<<"断开连接";

            });
        });
        //发送数据
        QString msg="1";
        tcp->write(msg.toUtf8());
    }

};


#endif // SOCKET_H
